package controllers

import (
	"net/http"
	"path/filepath"

	"github.com/flosch/pongo2"
	"github.com/gin-gonic/gin"
	"pets.ser163.cn/config"
	"pets.ser163.cn/model"
)

// @Summary 网站首页
// @Description 网站首页
// @Produce  http
// @Success 200 text
// @Router / [get]
func GetIndex(c *gin.Context) {
	tpl, err := pongo2.FromFile(filepath.Join(config.TemplateDir, "web/index.html"))
	if err != nil {
		c.String(http.StatusInternalServerError, err.Error())
		return
	}

	out, err := tpl.Execute(pongo2.Context{
		"title":   "宠物在线领养",
		"message": "欢迎来到宠物在线领养平台！在这里，您可以找到心仪的宠物并将它们带回家。",
	})
	if err != nil {
		c.String(http.StatusInternalServerError, err.Error())
		return
	}

	config.Log.Info("This is web test!!!")

	c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(out))
}

func GetPets(c *gin.Context) {
	pets := []model.Pet{
		{ID: 1, Name: "小汪", Description: "一只可爱的小狗", Image: "https://th.bing.com/th/id/R.0ac8db0557811b59e023cc5686a72bcb?rik=pSeUfzpFAuAIaQ&riu=http%3a%2f%2fwww.quazero.com%2fuploads%2fallimg%2f140305%2f1-140305223024.jpg&ehk=r78GawrcHRT2NUrUGn%2ffa0j%2fkTj8SQ9p%2fTuP%2fr8nT%2fM%3d&risl=&pid=ImgRaw&r=0"},
		{ID: 2, Name: "小花", Description: "一只可爱的小猫", Image: "https://img.zcool.cn/community/01f89e56ff66ba32f875a94479bc1b.jpg@1280w_1l_2o_100sh.jpg"},
		{ID: 3, Name: "小兔", Description: "一只可爱的小兔子", Image: "https://th.bing.com/th/id/R.76e8c3e345f766b48c828db9986b9b70?rik=768JtR%2b1MVY9iw&riu=http%3a%2f%2fwww.rabbitsos.org%2fPicture%2fphoto%2f680%2f1.jpg&ehk=mR%2bcvt4VHxBp5dpYtd1nszq96%2f3Ec9fy20apA7ENG8M%3d&risl=&pid=ImgRaw&r=0"},
	}

	tpl, err := pongo2.FromFile(filepath.Join(config.TemplateDir, "web/pets.html"))
	if err != nil {
		c.String(http.StatusInternalServerError, err.Error())
		return
	}

	out, err := tpl.Execute(pongo2.Context{
		"title": "可领养的宠物",
		"pets":  pets,
	})
	if err != nil {
		c.String(http.StatusInternalServerError, err.Error())
		return
	}

	c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(out))
}
